import { Injectable } from '@angular/core';
import { Response, Http, RequestOptionsArgs, Headers } from "@angular/http";
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';

//后台服务
@Injectable()
export class AppService {
    options?: RequestOptionsArgs ;
    url:string  = "http://www.powerps.cn:28008";
    client_id : string = "yycservice_acloud-admin";
    grant_type : string = "password";
    client_secret : string = "0074aac0-c7d9-40bb-81f5-3dbd9c3addfa";
  constructor(public http: Http) {
  }

  userLogin(data:any) {
    let  creds = "username=" + data.username + "&password=" + data.password + "&grant_type=" + this.grant_type + "&client_id=" + this.client_id + "&client_secret=" + this.client_secret;
    this.options = this.options || {};
        //添加授权
    this.options.headers = this.options.headers || new Headers();
    this.options.headers.set('Content-Type', 'application/x-www-form-urlencoded');
    this.options.headers.set('charset','UTF-8');
    return this.http.post(this.url + '/oauth/token',creds,this.options).map(res => res.json()).catch(this.handleError);
  }



  addUser(body: Object) {
    let bodyString = JSON.stringify(body); // Stringify payload
    return this.http.post(this.url + '/users', bodyString) // ...using post request
      .map((res: Response) => res.json()) // ...and calling .json() on the response to return data
      .catch(this.handleError); //...errors if any
  }



//获取用户信息
  getUserinfo() {
    return this.checkOauth();
  }

//检查认证
  checkOauth(){
    return this.http.get(this.url + '/oauth').map(res => res.json()).catch(this.handleError);
  }

//获取用户信息列表
  getPostuserList() {
    return this.http.get(this.url + '/base/getpostuserlist').map(res => res.json()).catch(this.handleError);
  }

//切换用户角色
  changePostuser(body: Object) {
    let bodyString = JSON.stringify(body);
    console.log(bodyString);
    return this.http.post(this.url + '/base/changepostuser',bodyString)
      .map((res: Response) => res).catch(this.handleError);

  }


//修改密码
  modifyPwd(pwd:any) {
    let bodyString = JSON.stringify({'oldpwd':pwd.opwd,'newpwd':pwd.npwd});
    return this.http.post(this.url + '/base/modifypassword',bodyString)
      .map(res => res)
      .catch(this.handleError);
  }

  handleError(error:any) {
    console.error("进去catch了:"+error);
    return Observable.throw(error.json().error || 'Server error');
  }


//待办
  getWaitDo() {
    return this.http.get(this.url + '/base/tasks')
      .map(res => res.json().tasks)
      .catch(this.handleError);
  }

    getNotices() {
    return this.http.get(this.url + '/oa/notice')
      .map(res => res.json().result)
      .catch(this.handleError);
  }

//取情况报告
  getOaCircsreport(taskguid:string) {
    return this.http.get(this.url + '/oa/report/' + taskguid)
      .map(res => res.json())
      .catch(this.handleError);
  }

//根据当前登录人获取菜单

  getMenus() {
    return this.http.get(this.url + '/base/menu/')
      .map(res => res.json())
      .catch(this.handleError);
  }
}
